﻿Imports <xmlns:log="urn:myLogConfig">

Public Class serveurUC

    Friend monServeur As Serveur
    Friend monServeurXML As XElement
    Friend treeViewSender As TreeViewItemWithIcons

    Private Sub BTN_save_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles BTN_save.Click
        'obtenir la fenetre principale
        Dim r = MainWindow.moiMeme

        Try
            monServeurXML.@nom = TBX_nom.Text
            monServeurXML.@ip = TBX_ip.Text
            monServeurXML.@nbJourRetention = TBX_NbJourRetention.Text

            Me.monServeur.reFill(monServeurXML)

            'Mettre à jour l'arbre
            Me.treeViewSender.Tag = monServeur
            Me.treeViewSender.tagXML = monServeurXML
            Me.treeViewSender.HeaderText = monServeur.nom

            'Sauvegarder les changement dans le fichier
            r.masterDoc.Save(r.xmlFileName)

            'Afficher message succes
            r.consoleOut.addItim(New ConsoleTypes.ok("Modification du serveur: Succès", monServeurXML.@nom))

        Catch ex As Exception
            r.consoleOut.addItim(New ConsoleTypes.erreur("Une erreur est apparue lors de la modification du serveur", ex.Message))
        End Try
    End Sub

    Friend Sub setInfo(srv As Serveur, sender As TreeViewItemWithIcons)
        monServeur = srv
        treeViewSender = sender
        monServeurXML = Me.treeViewSender.tagXML

        TBX_nom.Text = monServeur.nom
        TBX_ip.Text = monServeur.ip
        TBX_NbJourRetention.Text = monServeur.nbJourRetention
    End Sub

    Private Sub BTN_serveurDelete_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles BTN_serveurDelete.Click

        If MessageBox.Show("Supprimer le serveur supprimera également tous ses dossiers, continuer ?", "Confirmation", MessageBoxButton.YesNo) = MessageBoxResult.Yes Then

            'obtenir la fenetre principale
            Dim r = MainWindow.moiMeme

            Try

                monServeurXML.Remove()
                r.panelDynamic.Children.Clear()

                'Sauvegarder les changement dans le fichier
                r.masterDoc.Save(r.xmlFileName)

                'recharger treeView
                r.readXML()

                'Afficher message succes
                r.consoleOut.addItim(New ConsoleTypes.ok("Suppression du serveur: Succès", monServeurXML.@nom & " supprimé"))

            Catch ex As Exception
                r.consoleOut.addItim(New ConsoleTypes.erreur("Une erreur est apparue lors de la suppression du serveur", ex.Message))
            End Try

        End If

    End Sub

    Private Sub BTN_cheminAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles BTN_cheminAdd.Click

        'obtenir la fenetre principale
        Dim r = MainWindow.moiMeme

        Try

            'Rechercher le noeud serveur
            Dim querySrv = (From ele In r.masterDoc...<log:serveur>
                            Select ele Where ele.Parent.@nom = monServeur.pltParent.nom And ele.@nom = monServeur.nom).First

            Dim newCheminNode As New XElement(XElement.Parse(xmlTemplates.newchemin))
            newCheminNode.SetAttributeValue("xmlns", Nothing)
            querySrv.Add(newCheminNode)

            'Sauvegarder les changement dans le fichier
            r.masterDoc.Save(r.xmlFileName)

            ' cree le noeud xml et treeview
            Dim unChemin As New Chemin(newCheminNode, monServeur)
            Dim noeudChemin As New TreeViewItemWithIcons
            noeudChemin.Icon = My.Resources.folder.ToImageSource
            noeudChemin.HeaderText = unChemin.destDossier
            noeudChemin.Tag = unChemin

            treeViewSender.Items.Add(noeudChemin)

            'surligner le nouveau noeud
            treeViewSender.IsExpanded = True
            noeudChemin.IsSelected = True
            noeudChemin.BringIntoView()

            'Afficher message succes
            r.consoleOut.addItim(New ConsoleTypes.ok("Ajout du chemin: Succès", newCheminNode.@destdossier))

        Catch ex As Exception
            r.consoleOut.addItim(New ConsoleTypes.erreur("Une erreur est apparue lors de l'ajout du chemin", ex.Message))
        End Try

    End Sub

    Private Sub TBX_NbJourRetention_onPreviewtextInput(sender As System.Object, e As System.Windows.Input.TextCompositionEventArgs) Handles TBX_NbJourRetention.PreviewTextInput

        e.Handled = Not AreAllValidNumericChars(e.Text)
        'MyBase.OnPreviewTextInput(e)

    End Sub

    Private Function AreAllValidNumericChars(str As String) As Boolean
        For Each c As Char In str
            If Not [Char].IsNumber(c) Then
                Return False
            End If
        Next

        Return True
    End Function

End Class
